<template>
  <div class="roleManagement-container">
    <el-divider content-position="left">
      {{ paperDV.paper.title }} {{ paperDV.count }}/{{ paperDV.toReadCount }}
    </el-divider>
    <vab-query-form>
      <vab-query-form-left-panel :span="12"></vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="account.displayName"
        label="考生"
      ></el-table-column>
      <el-table-column show-overflow-tooltip prop="createDate" label="答卷时间">
        <template #default="{ row }">
          {{ formatDate(row.createDate) }}
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="paper.remark"
        label="得分=客观+主观"
        width="150"
      >
        <template #default="{ row }">
          {{ row.score }} = {{ jsex.objectGet(row.objectiveScore, '0') }} +
          {{ jsex.objectGet(row.subjectiveScore, '0') }}
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="80">
        <template #default="{ row }">
          <el-button v-if="row.resolve" type="info" @click="handleEdit(row)">
            查看
          </el-button>
          <el-button v-else type="success" @click="handleEdit(row)">
            评阅
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <edit ref="edit" :id-key="idkey" @fetch-data="fetchData"></edit>
  </div>
</template>

<script>
  import { answerSheet, getPaper } from '/src/api/grade'
  import Edit from './components/ManagementEdit'
  import { defaultPageSize } from '/src/config'
  import { formatDate } from '/src/utils/formatDateTime'
  import * as jsex from '/src/utils/jsex'

  export default {
    name: 'Management',
    components: { Edit },
    data() {
      return {
        idkey: 'id',
        jsex: jsex,
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        paperDV: { paper: {} },
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: defaultPageSize,
          order: {
            //  sort: 'asc'
          },
        },
      }
    },
    computed: {
      paperId() {
        return this.$route.params.paperId
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      formatDate: formatDate,
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        console.log(row)
        let { href } = this.$router.resolve({
          path: `/gradeAnswerSheet/${row.id}`,
        })
        window.open(href, '_blank')
      },
      handleDelete(row) {
        if (row[this.idkey]) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row[this.idkey] })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item[this.idkey]).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true

        const resp1 = await getPaper(this.paperId)
        this.paperDV = resp1.data
        const resp = await answerSheet(this.paperId)
        console.log(resp)
        const data = resp.data

        this.list = data

        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
